package com.wj.Pic;


import org.junit.Test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @author WangJin
 * @version 1.0
 * @date 2022/1/27 14:55
 */
public class AscPic {

        @Test
        public void PcTest() {
            change(new File("D:\\io\\pc\\1.jpg"), "jpg", "D:\\io\\pc\\out\\");
        }

        public void change(File f, String imageType, String out) {
            BufferedImage bi = null;
            try {
                bi = ImageIO.read(f);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
            int width = bi.getWidth();
            int height = bi.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
            // 获取画笔
            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, height);
            int flag = 0;
            for (int i = 0; i < width; i += 13) {
                for (int j = 0; j < height; j += 13) {
                    int rgb = bi.getRGB(i, j);
                    int r = (rgb >> 16) & 0xFF;
                    int g = (rgb >> 8) & 0xFF;
                    int b = rgb & 0xFF;
                    // 获取灰度值
//				int gray = (int)Math.pow((Math.pow(r, 2.2) + Math.pow(1.5 * g, 2.2) + Math.pow(0.6 * b, 2.2)) / (1 + Math.pow(1.5, 2.2) + Math.pow(0.6, 2.2)), (1/2.2));
                    Color c = new Color(r, g, b);
                    graphics.setColor(c);
                    graphics.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 12));
//				int gray = (int) (0.299f * r + 0.578f * g + 0.114f * b);
//				int index = (int) Math.floor(gray*ch.length()/256);
                    if (i % 2 == 0) {
                        graphics.drawString("王", i, j);
                    } else {
                        graphics.drawString("劲", i, j);
                    }
                }
                System.out.println();
            }
            try {
                File dir = new File(out);
                dir.mkdirs();
                File file = new File(out + "/" + f.getName());
                ImageIO.write(image, imageType, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}
